package ee.serge.server.template;

import java.util.List;

import ee.serge.shared.HandledException;
import ee.serge.shared.Loan;

public class FullLoansTemplate extends Template {

   public FullLoansTemplate(String query) throws Exception {
      super(query);
   }

   @Override
   protected String getTitle() {
      return "All loans";
   }

   @Override
   protected String getHeader() {
      return "All loans";
   }

   @Override
   protected String getHeader2() {
      return "Full list of all offers";
   }

   @Override
   protected String getContent() {
      List<Loan> loans = null;
      try {
         loans = publicServiceImpl.getLoans();
      } catch (HandledException e) {
         return "System error";
      }
      StringBuffer content = new StringBuffer();
      content.append("<table>");
      content.append("<tr><th>Loan name</th><th>Description</th><th>APR</th><th>Transfer fee</th><th>Rating</th><th></th></tr>");
      for (Loan loan : loans) {
         content.append("<tr>");
         content.append("<td><h4>" + loan.getName() + "</h4></td>");
         content.append("<td>" + loan.getComment() + "</td>");
         content.append("<td>" + loan.getAPR() + "%</td>");
         content.append("<td>£" + loan.getTransferFee() + "</td>");
         int round = (int) ((float)(loan.getTotalRating()) / loan.getFeedbackCount());
         String rating = loan.getFeedbackCount() == 0 ? "" : round + "/5";
         content.append("<td>" + rating + "</td>");
         content.append("<td><a href='#!loan?id=" + loan.getId() + "'>Read more about " + loan.getName() + "</a></td>");
      }
      content.append("</table>");
      return content.toString();
   }

   @Override
   protected void init() throws Exception {
   }
   
}
